package cn.community.care.module.system.service.msg;

import cn.community.care.module.system.controller.admin.msg.vo.MsgPageReqVO;
import cn.community.care.module.system.controller.admin.msg.vo.MsgRespDetailVO;
import cn.community.care.module.system.controller.admin.msg.vo.MsgSaveReqVO;
import cn.community.care.module.system.dal.dataobject.msg.MsgDO;
import cn.community.care.module.system.dal.dataobject.user.AdminUserDO;
import cn.community.care.module.system.dal.mysql.msg.MsgMapper;
import cn.community.care.module.system.dal.mysql.user.AdminUserMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.community.care.framework.common.pojo.PageResult;
import cn.community.care.framework.common.pojo.PageParam;
import cn.community.care.framework.common.util.object.BeanUtils;

import javax.annotation.Resource;

import static cn.community.care.framework.common.exception.util.ServiceExceptionUtil.exception;
/**
 * 评论 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MsgServiceImpl implements MsgService {

    @Resource
    private MsgMapper msgMapper;

    @Resource
    private AdminUserMapper userMapper;

    @Override
    public Long createMsg(MsgSaveReqVO createReqVO) {
        //
        if ( createReqVO.getParentCommentId()!=null&&createReqVO.getParentCommentId()!=0){
            MsgDO parentMsg = msgMapper.selectById(createReqVO.getParentCommentId());
            createReqVO.setParentCommentId(parentMsg.getId());
        }
        AdminUserDO adminUserDO = userMapper.selectById(createReqVO.getCommenterId());
        // 插入
        MsgDO msg = BeanUtils.toBean(createReqVO, MsgDO.class);
        msg.setCommenterAvatar(adminUserDO.getAvatar());
        msg.setCommenterName(adminUserDO.getNickname());
        msgMapper.insert(msg);
        // 返回
        return msg.getId();
    }

    @Override
    public void updateMsg(MsgSaveReqVO updateReqVO) {
        // 校验存在
        validateMsgExists(updateReqVO.getId());
        AdminUserDO adminUserDO = userMapper.selectById(updateReqVO.getCommenterId());
        // 更新
        MsgDO updateObj = BeanUtils.toBean(updateReqVO, MsgDO.class);
        updateObj.setCommenterAvatar(adminUserDO.getAvatar());
        updateObj.setCommenterName(adminUserDO.getNickname());
        msgMapper.updateById(updateObj);
    }

    @Override
    public void deleteMsg(Long id) {
        // 校验存在
        validateMsgExists(id);
        // 删除
        msgMapper.deleteById(id);
    }

    private void validateMsgExists(Long id) {
        if (msgMapper.selectById(id) == null) {
//            throw exception(MSG_NOT_EXISTS);
        }
    }

    @Override
    public MsgDO getMsg(Long id) {
        return msgMapper.selectById(id);
    }

    @Override
    public PageResult<MsgDO> getMsgPage(MsgPageReqVO pageReqVO) {
        return msgMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MsgRespDetailVO> getMsgByArticelId(Long id) {
        List<MsgDO> msgDOS = msgMapper.selectList(new QueryWrapper<MsgDO>().eq("article_id", id).eq("parent_comment_id", 0));
        //查出来的msgDOS中评论的评论的会有子评论，所以这里就要先查父的
        List<MsgRespDetailVO> msgRespDetailVOS = BeanUtils.toBean(msgDOS, MsgRespDetailVO.class);
        msgRespDetailVOS.forEach(msg -> {
            List<MsgDO> childMsg = msgMapper.selectList(new QueryWrapper<MsgDO>().eq("article_id", id).eq("parent_comment_id", msg.getId()));
            List<MsgRespDetailVO> subMsgRespDetailVOS = BeanUtils.toBean(childMsg, MsgRespDetailVO.class);
            msg.setSubComment(subMsgRespDetailVOS);
        });
        return msgRespDetailVOS;
    }

}